package com._58city.spark.app.ext;

import java.util.concurrent.atomic.AtomicBoolean;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;

import com._58city.spark.app.ext.dao.BelongCateDao;
import com._58city.spark.app.ext.dao.DispCateDao;
import com._58city.spark.app.ext.dao.DispCityDao;
import com._58city.spark.app.ext.dao.ImcInfoSourceDao;
import com._58city.spark.app.ext.dao.ResumeSourceDao;

/**
 * @author lihao Dao工具类，方便使用城市和cate字典表
 */
public class DaoUtil {

	private static Logger LOG = LoggerFactory.getLogger(DaoUtil.class);

	private static AtomicBoolean start_sign = new AtomicBoolean(false);

	private static DispCateDao dispCateDao;
	private static DispCityDao dispCityDao;
	private static BelongCateDao belongCateDao;
	private static ImcInfoSourceDao imcInfoSourceDao;
	private static ResumeSourceDao resumeSourceDao;

	/**
	 * 根据spring容器初始化城市和cate字典表Dao的方法
	 * 
	 * @param context
	 */
	public synchronized static void init(ApplicationContext context) {
		if (!start_sign.get()) {
			LOG.info("--- DaoUtil init ---");
			dispCateDao = context.getBean(DispCateDao.class);// 展现类别字典
			dispCityDao = context.getBean(DispCityDao.class);// 展现城市字典
			belongCateDao = context.getBean(BelongCateDao.class);// 归属类别字典
			imcInfoSourceDao = context.getBean(ImcInfoSourceDao.class);// IMC来源平台字典
			resumeSourceDao = context.getBean(ResumeSourceDao.class);// 简历来源表
			start_sign.compareAndSet(false, true);
		}
	}

	/**
	 * 得到cate字典Dao
	 * 
	 * @return
	 */
	public static DispCateDao getDispCateDao() {
		if (!start_sign.get())
			throw new IllegalStateException("DaoUtil is not init!");
		return dispCateDao;
	}

	/**
	 * 得到城市字典表Dao
	 * 
	 * @return
	 */
	public static DispCityDao getDispCityDao() {
		if (!start_sign.get())
			throw new IllegalStateException("DaoUtil is not init!");
		return dispCityDao;
	}

	/**
	 * 得到归属cate字典Dao
	 * 
	 * @return
	 */
	public static BelongCateDao getBelongCateDao() {
		if (!start_sign.get())
			throw new IllegalStateException("DaoUtil is not init!");
		return belongCateDao;
	}

	/**
	 * 得到IMC来源字典Dao
	 * 
	 * @return
	 */
	public static ImcInfoSourceDao getImcInfoSourceDao() {
		if (!start_sign.get())
			throw new IllegalStateException("DaoUtil is not init!");
		return imcInfoSourceDao;
	}

	public static ResumeSourceDao getResumeSourceDao() {
		if (!start_sign.get())
			throw new IllegalStateException("DaoUtil is not init!");
		return resumeSourceDao;
	}

}
